// pages/order/order.js
const app = getApp()

Page({
  data: {
    orderList: [],
    currentTab: 'all', // all: 全部, pending: 待处理, processing: 进行中, completed: 已完成
    orderStatus: {
      pending: '待接单',
      taken: '已接单',
      processing: '进行中',
      completed: '已完成',
      cancelled: '已取消'
    }
  },

  onLoad() {
    this.checkLoginStatus()
    this.loadOrderData()
  },

  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
  },

  checkLoginStatus() {
    if (!app.globalData.hasUserInfo) {
      wx.redirectTo({
        url: '/pages/login/login'
      })
    }
  },

  switchTab(e) {
    const tab = e.currentTarget.dataset.tab
    this.setData({
      currentTab: tab
    })
    this.filterOrders(tab)
  },

  loadOrderData() {
    // 模拟订单数据
    const mockOrderData = [
      {
        id: 1,
        type: 'take',
        title: '帮忙取快递',
        price: 5,
        status: 'pending',
        createTime: '2024-01-15 10:30',
        location: '南门快递点',
        destination: '3号楼201',
        customer: { name: '张同学', avatar: '/images/avatar1.png' },
        taker: null
      },
      {
        id: 2,
        type: 'take',
        title: '代买午餐',
        price: 8,
        status: 'taken',
        createTime: '2024-01-15 09:15',
        location: '食堂一楼',
        destination: '图书馆3楼',
        customer: { name: '李同学', avatar: '/images/avatar2.png' },
        taker: { name: '我', avatar: app.globalData.userInfo.avatarUrl }
      },
      {
        id: 3,
        type: 'buy',
        title: '文档打印服务',
        price: 3,
        status: 'processing',
        createTime: '2024-01-14 16:20',
        location: '文印中心',
        destination: '教学楼101',
        customer: { name: '我', avatar: app.globalData.userInfo.avatarUrl },
        taker: { name: '王同学', avatar: '/images/avatar3.png' }
      },
      {
        id: 4,
        type: 'take',
        title: '物品租借',
        price: 10,
        status: 'completed',
        createTime: '2024-01-14 14:00',
        location: '宿舍楼',
        destination: '操场',
        customer: { name: '赵同学', avatar: '/images/avatar4.png' },
        taker: { name: '我', avatar: app.globalData.userInfo.avatarUrl }
      },
      {
        id: 5,
        type: 'buy',
        title: '学习辅导',
        price: 20,
        status: 'cancelled',
        createTime: '2024-01-13 19:30',
        location: '自习室',
        destination: '线上',
        customer: { name: '我', avatar: app.globalData.userInfo.avatarUrl },
        taker: null
      }
    ]

    this.setData({
      orderList: mockOrderData
    })
    this.filterOrders(this.data.currentTab)
  },

  filterOrders(tab) {
    let filteredOrders = this.data.orderList
    
    if (tab !== 'all') {
      filteredOrders = this.data.orderList.filter(order => order.status === tab)
    }
    
    this.setData({
      filteredOrders: filteredOrders
    })
  },

  viewOrderDetail(e) {
    const orderId = e.currentTarget.dataset.id
    const order = this.data.orderList.find(item => item.id === orderId)
    
    if (order) {
      wx.navigateTo({
        url: `/pages/order/detail/detail?id=${orderId}`
      })
    }
  },

  takeOrder(e) {
    const orderId = e.currentTarget.dataset.id
    this.updateOrderStatus(orderId, 'taken')
  },

  startOrder(e) {
    const orderId = e.currentTarget.dataset.id
    this.updateOrderStatus(orderId, 'processing')
  },

  completeOrder(e) {
    const orderId = e.currentTarget.dataset.id
    this.updateOrderStatus(orderId, 'completed')
  },

  cancelOrder(e) {
    const orderId = e.currentTarget.dataset.id
    wx.showModal({
      title: '确认取消',
      content: '确定要取消这个订单吗？',
      success: (res) => {
        if (res.confirm) {
          this.updateOrderStatus(orderId, 'cancelled')
        }
      }
    })
  },

  updateOrderStatus(orderId, newStatus) {
    const updatedList = this.data.orderList.map(order => {
      if (order.id === orderId) {
        return { ...order, status: newStatus }
      }
      return order
    })

    this.setData({
      orderList: updatedList
    })
    this.filterOrders(this.data.currentTab)

    wx.showToast({
      title: '操作成功',
      icon: 'success'
    })
  },

  createOrder() {
    wx.navigateTo({
      url: '/pages/order/create/create'
    })
  },

  onPullDownRefresh() {
    this.loadOrderData()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  }
})